package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.Help;
import org.example.system.entity.Remark;
import org.example.system.entity.OperateLog;
import org.example.system.entity.User;
import org.example.system.mapper.RemarkMapper;
import org.example.system.service.RemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 评论服务层实现类
 */
@Service
public class RemarkServiceImpl implements RemarkService {
    @Autowired
    private RemarkMapper remarkMapper;

    /**
     * 按条件分页查询
     * @param remark
     * @return
     */
    @Override
    public IPage<Remark> selectPage(Remark remark){
        MPJLambdaWrapper<Remark> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Remark.class);
        wrapper.leftJoin(Help.class, Help::getId,Remark::getHelpId);
        wrapper.leftJoin(User.class, User::getId,Remark::getUserId);
        //条件
        if (remark.getHelpId() != null){
            wrapper.eq(Remark::getHelpId,remark.getHelpId());
        }
        if (remark.getUserId() != null){
            wrapper.eq(Remark::getUserId,remark.getUserId());
        }
        wrapper.selectAs(Help::getTitle,Remark::getHelpTitle);
        wrapper.selectAs(User::getRealname,Remark::getRealname);
        wrapper.selectAs(User::getAvatar,Remark::getAvatar);
        wrapper.selectAs(User::getRole,Remark::getRole);
        if (StrUtil.isNotEmpty(remark.getHelpTitle())){
            wrapper.like(Help::getTitle,remark.getHelpTitle());    //相似的新闻标题（模糊查询）
        }
        if (StrUtil.isNotEmpty(remark.getCreateBy())){
            wrapper.like(Remark::getCreateBy,remark.getCreateBy());    //相似的发布用户名（模糊查询）
        }

        if (StrUtil.isNotEmpty(remark.getRealname())){
            wrapper.like(User::getRealname,remark.getRealname());    //相似的真实姓名（模糊查询）
        }
        wrapper.orderByDesc(OperateLog::getId);
        IPage<Remark> page = new Page<>();
        page.setCurrent(remark.getPageNum());
        page.setSize(remark.getPageSize());
        return remarkMapper.selectPage(page,wrapper);
    }

    /**
     * 添加评论
     * @param remark
     * @return
     */
    @Override
    public int add(Remark remark) {
        remark.setCreateTime(new Date());
        return remarkMapper.insert(remark);
    }

    /**
     * 修改
     * @param remark
     * @return
     */
    @Override
    public int update(Remark remark) {
        remark.setUpdateTime(new Date());
        return remarkMapper.updateById(remark);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return remarkMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public Remark getOne(Long id) {
        return remarkMapper.selectById(id);
    }
}
